package com.stone.bigdata.controller;

import com.stone.bigdata.domain.VO.CityTotalVO;
import com.stone.bigdata.domain.VO.TodayAddVO;
import com.stone.bigdata.domain.VO.UserStateVO;
import com.stone.bigdata.service.ProductListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
public class ProductListController {

    @Autowired
    private ProductListService productListService;

    @RequestMapping(value = "/api/getTodayAdd", method = RequestMethod.GET)
    public TodayAddVO getTodayAdd() {
        LocalDateTime nowLocal = LocalDateTime.now();
        Timestamp zero = Timestamp.valueOf(LocalDateTime.of(nowLocal.toLocalDate(), LocalTime.MIN));
        //System.out.println("当天0点的时间戳" + zero);
        Timestamp now = Timestamp.valueOf(nowLocal);
        //System.out.println("现在的时间戳" + now);
        Integer todayAdd = productListService.getTotalAdd(zero, now);
        //System.out.println("今日新增总数" + todayAdd);
        TodayAddVO todayAddVO = new TodayAddVO();
        todayAddVO.setTodayTotalAdd(todayAdd);
        Integer todayAverageAdd = todayAdd / ( nowLocal.getHour() + 1 );
        todayAddVO.setTodayAverageAdd(todayAverageAdd);
        //System.out.println("今日平均新增" + todayAverageAdd);
        return  todayAddVO;
    }

    @RequestMapping(value = "/api/getCityDevTotal", method = RequestMethod.GET)
    public List<CityTotalVO> getCityDevTotal() {
        List<CityTotalVO> list = productListService.listCityTotal();
//        list.forEach(cityTotalVO -> {
//            System.out.println(cityTotalVO.toString());
//        });
        return list;
    }

    @RequestMapping(value = "/api/getActiveCity", method = RequestMethod.GET)
    public List<CityTotalVO> getActiveCity() {
        LocalDateTime yesterday = LocalDateTime.now().minusDays(1);
        // 昨天00:00:00
        Timestamp yesterdayZero = Timestamp.valueOf(LocalDateTime.of(yesterday.toLocalDate(), LocalTime.MIN));
        // 昨天23:59:59
        Timestamp yesterdayEnd = Timestamp.valueOf(LocalDateTime.of(yesterday.toLocalDate(), LocalTime.MAX));
        System.out.println("zero: " + yesterdayZero + "end: " + yesterdayEnd);
        return productListService.listCityActiveTotal(yesterdayZero, yesterdayEnd);
    }

    @RequestMapping(value = "/api/getUserState", method = RequestMethod.GET)
    public List<List<String>> getUserState() {
        LocalDateTime now = LocalDateTime.now();
        String nowStr = DateTimeFormatter.ofPattern("HH:mm:ss").format(now);
        //System.out.println("nowStr=" + nowStr);
        List<UserStateVO> userStateVO = productListService.listUserState();
        List<List<String>> result = new ArrayList<>();
        String basic = "**********";
        userStateVO.forEach( item -> {
            //System.out.println(item.toString());
            List<String> temp = new ArrayList<>();
            temp.add(basic + item.getUniqueId());
            temp.add(nowStr);
            if(item.getState().equals("0")) {
                temp.add("离线");
            } else {
                temp.add("上线");
            }
            result.add(temp);
        });
        return result;
    }

    @RequestMapping(value = "/api/getTodayHoursAdd", method = RequestMethod.GET)
    public List<Integer> getTodayHoursAdd() {
        return productListService.listHoursAdd();
    }

    @RequestMapping(value = "/api/getTotal", method = RequestMethod.GET)
    public Integer getTotal() {
        return productListService.getTotal();
    }
}
